package net.portly.cst;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.portly.cst.flex.FlexItemType;

public abstract class MetaAssetContainer implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Map<FlexItemType, List<StructuralAssetInstance>> structuralAssetMap = new HashMap<FlexItemType, List<StructuralAssetInstance>>();
	protected Map<FlexItemType, AssetTypeDescriptor> flexItemsByAssetType = new HashMap<FlexItemType, AssetTypeDescriptor>();

	public MetaAssetContainer() {
		super();
	}

	public void putTypeWithAssetTypeDescriptor(AssetTypeDescriptor atd) {
		flexItemsByAssetType.put(atd.getWhatTypeOfFlexItem(), atd);
	}

	public AssetTypeDescriptor getAssetTypeDescriptorForFlexItem(FlexItemType flexItem) {
		return flexItemsByAssetType.get(flexItem);
	}

	public void addInstancesListToStructuralAssetMap(FlexItemType flexItemTypeWeBuilt, List<StructuralAssetInstance> structuralAssetList) {
		this.structuralAssetMap.put(flexItemTypeWeBuilt, structuralAssetList);
	}
	
	protected String toStringForAllForStructuralFlexItem(FlexItemType flexItem, String prePend) {
		AssetTypeDescriptor atd = flexItemsByAssetType.get(flexItem);
		return prePend + (atd == null ? " NULL" : atd.getAssettype());
	}

	public String toString() {
		return "FlexFamily: [ " + "\n  " + toStringForAllForStructuralFlexItem(FlexItemType.ATTRIBUTE, "Attribute: ") + "\n  " + toStringForAllForStructuralFlexItem(FlexItemType.PARENTDEF, "Parent Def: ") + "\n  "
				+ toStringForAllForStructuralFlexItem(FlexItemType.FILTER, "Filter: ") + "\n  " + toStringForAllForStructuralFlexItem(FlexItemType.PARENT, "Parent: ") + "\n  " + toStringForAllForStructuralFlexItem(FlexItemType.FLEXDEF, "Flex Def: ")
				+ "\n  " + toStringForAllForStructuralFlexItem(FlexItemType.FLEXASSET, "Asset: ") + "\n]";
	}

	public String toStringCD() {
		return "  [ " + toStringForAllForStructuralFlexItem(FlexItemType.FLEXDEF, "Flex Def: ") + "\n" + " ]";
	}

	public String toStringSA(FlexItemType what) {
		StringBuilder sb = new StringBuilder("\n" + what.getType() + ":\n");
		
		List<StructuralAssetInstance> saList = structuralAssetMap.get(what);
		
		if (saList != null) {
			for (StructuralAssetInstance flexDefInstance : saList) {
				sb.append (flexDefInstance.toString());
			}
		}
		return sb.toString();
	}
	
	public abstract String getFamilyName ();

}